package ilarele.planner.Activities;

import ilarele.planner.Adapters.Cell;
import ilarele.planner.Adapters.GridViewCellAdapter;
import ilarele.planner.Constants.ConstantValues;
import ilarele.planner.Project.Project;
import ilarele.planner.Project.Task;
import ilarele.planner.Scrolls.ListViewOnScrollListener;
import ilarele.planner.Scrolls.MyHorizontalScroll;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnScrollChangedListener;
import android.widget.GridView;

public class AnPlanTest3 extends Activity implements
		OnScrollChangedListener
{

	// menu entries
	private static final int MENU_ADD_TASK = 0;

	// GUI
	private GridViewCellAdapter gridview_cell_adapter;
	private MyHorizontalScroll listview_h_scroll;
	private GridView gridview;
	
	// model
	private Project project;
	private ArrayList<Cell> cells; 
	

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		project = new Project();
		cells = new ArrayList<Cell>();
		gridview_cell_adapter = new GridViewCellAdapter(this, R.layout.task_layout, cells,
				project);
		
	    gridview = (GridView) findViewById(R.id.gridview);
	    gridview.setAdapter(gridview_cell_adapter);
		addDummyTasks(5);

		gridview.setOnScrollListener(new ListViewOnScrollListener());
		
	}

	private void addDummyTasks(int n)
	{
		for (int i = 0; i < n; i++)
		{
			Calendar start = ConstantValues.getTodayCalendar();
			start.set(110, 9, 2 + i);
			Task new_dummy = new Task(getTasks().size(), start);
			addTask(new_dummy);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add(0, MENU_ADD_TASK, 0, R.string.menu_add_task).setIcon(
				android.R.drawable.ic_menu_add);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		super.onOptionsItemSelected(item);
		switch (item.getItemId())
		{
		case MENU_ADD_TASK:
			Task new_dummy = new Task(getTasks().size());
			addTask(new_dummy);
			return true;
		}

		return false;
	}

	private void addTask(Task new_dummy)
	{
		// model
		project.addTask(new_dummy);
		
		// view
		int crt_duration = project.getDuration();
		gridview.setNumColumns(crt_duration);
		
		// TODO PX -> DIP
		gridview.getLayoutParams().height = project.getAllTasks().size() * ConstantValues.DAY_HEIGHT_DP;
		gridview.getLayoutParams().width = crt_duration * ConstantValues.DAY_WIDTH_DP;
		constructTasks();
		gridview_cell_adapter.notifyDataSetChanged();
		
	}

	private void constructTasks()
	{
		Calendar prj_finish = project.getFinishDate();
		Calendar prj_start = project.getStartDate();
		Calendar task_start;
		Calendar task_finish;
		
		int before_col;
		int task_col;
		int after_col;
		
		cells.clear();
		for (Task task: project.getAllTasks())
		{
			// TODO do not recalculate for all of them
			task_start = task.getStartDate();
			task_finish = task.getFinishDate();
			
			before_col = ConstantValues.getDiff(task_start, prj_start);
			task_col = task.getDuration();
			after_col = ConstantValues.getDiff(prj_finish, task_finish);
			
			// fill before
			addNullCells(before_col);
			// fill in
			for (int i = 0; i < task_col; i++)
			{
				cells.add(new Cell());
			}
			// fill after
			addNullCells(after_col);
			
		}
	}

	private void addNullCells(int n)
	{
		for (int i = 0; i < n; i++)
		{
			cells.add(null);
		}
	}

	private ArrayList<Task> getTasks()
	{
		return project.getAllTasks();
	}

	@Override
	public void onScrollChanged()
	{
//		Log.d("log", "onScrollChanged");

	}


}
